import time

from Email import Email
from MemoryMonitor import MemoryMonitor


class CentralizedMonitor:
    CHECK_INTERVAL_SEC = 60 * 30

    def __init__(self, mem_monitors: list):
        self.mms = mem_monitors

    def loop(self, email_report=1):
        count = 0
        while True:
            summarise = '--- RUNNING FOR %0.2f min--- \n' % (count * CentralizedMonitor.CHECK_INTERVAL_SEC / 60.0)
            for mm in self.mms:
                m: MemoryMonitor = mm
                m.diagnose()
                m.mem_check_oom()
                summarise += m.__str__()
                summarise += '\n'
            if count % email_report == 0:
                Email.send('ole_lee.askey.com', '[AUTO-TEST]CDR7010 OOM MONITOR UPDATES', summarise)
            print(summarise)
            count += 1

            time.sleep(CentralizedMonitor.CHECK_INTERVAL_SEC)
